
// <copyright file="TextReaderEnumerator.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The text reader enumerator.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;

    /// <summary>
    /// The text reader enumerator.
    /// </summary>
    public class TextReaderEnumerator : IEnumerator<string>
    {
        #region Fields

        /// <summary>
        /// The reader.
        /// </summary>
        private readonly TextReader _reader;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="TextReaderEnumerator"/> class.
        /// </summary>
        /// <param name="reader">
        /// The reader.
        /// </param>
        public TextReaderEnumerator(TextReader reader)
        {
            this._reader = reader;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        public string Current { get; private set; }

        #endregion Public Properties

        #region Explicit Interface Properties

        /// <summary>
        /// Gets the current.
        /// </summary>
        object IEnumerator.Current
        {
            get
            {
                return this.Current;
            }
        }

        #endregion Explicit Interface Properties

        #region Public Methods and Operators

        /// <summary>
        /// The dispose.
        /// </summary>
        public void Dispose()
        {
            this._reader.Dispose();
        }

        /// <summary>
        /// The move next.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool MoveNext()
        {
            if (this._reader.Peek() != -1)
            {
                string line = this._reader.ReadLine();
                this.Current = string.IsNullOrEmpty(line) ? null : line;
                return true;
            }

            this._reader.Close();
            this.Current = null;
            return false;
        }

        /// <summary>
        /// The reset.
        /// </summary>
        public void Reset()
        {
        }

        #endregion Public Methods and Operators
    }
}
